package gui;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import model.DailyIrregular;
import model.DailyRegular;
import model.PN;
import model.Patient;
import model.Prescription;

import service.Service;

public class ShowPrescriptionDialog extends JDialog {
	private Patient currentPatient;
	private Prescription currentPrescription = null;
	private JList listPrescriptions;
	private JScrollPane scollPrescriptions;
	private JLabel labelPrescription;
	private JPanel panelPrescription;
	private JPanel panelPN;
	private JPanel panelIrregular;
	private JPanel panelRegular;
	private JButton usePN;
	private JLabel labelPatient;
	private JLabel labelStart;
	private JLabel labelEnd;
	private JLabel labelDrug;
	private JLabel labelDayDose;
	private JLabel labelTotalDose;
	private JLabel labelPNDose;
	private JLabel labelUsed;
	private JLabel errorLabel;
	private JTextField txtStart;
	private JTextField txtEnd;
	private JTextField txtDrug;
	private JTextField txtDayDose;
	private JTextField txtTotalDose;
	private JTextField txtPNDose;
	private JTextField txtUsed;
	private JTextField txtUsedDate;
	private JTextField txtMorning;
	private JTextField txtNoon;
	private JTextField txtEvening;
	private JTextField txtNight;
	private JList listDoses;
	private JScrollPane scrollDoses;

	private Controller controller = new Controller();

	public ShowPrescriptionDialog(Patient patient) {
		currentPatient = patient;

		// the frame's own attributes...
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setModal(true);
		this.setLayout(null);
		this.setTitle("Show prescriptions");
		this.setLocation(150, 150);
		this.setSize(600, 360);

		labelPrescription = new JLabel("Choose prescription");
		labelPrescription.setSize(160, 22);
		labelPrescription.setLocation(20, 40);
		this.add(labelPrescription);
		listPrescriptions = new JList();
		listPrescriptions.setListData(currentPatient.getPrescriptions().toArray());
		this.add(listPrescriptions);
		listPrescriptions.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		scollPrescriptions = new JScrollPane(listPrescriptions);
		scollPrescriptions.setLocation(20, 80);
		scollPrescriptions.setSize(160, 190);
		this.add(scollPrescriptions);

		String p = "Patient: " + currentPatient.getCprno() + " " + currentPatient.getName() + " "
				+ currentPatient.getWeight() + " kg.";
		labelPatient = new JLabel(p);
		this.add(labelPatient);
		labelPatient.setLocation(20, 15);
		labelPatient.setSize(300, 25);

		panelPrescription = new JPanel();
		panelPrescription.setLayout(null);
		this.add(panelPrescription);
		panelPrescription.setSize(400, 180);
		panelPrescription.setLocation(300, 15);
		panelPrescription.setVisible(false);

		labelStart = new JLabel("Starttime");
		panelPrescription.add(labelStart);
		labelStart.setSize(120, 25);
		labelStart.setLocation(10, 10);

		txtStart = new JTextField();
		panelPrescription.add(txtStart);
		txtStart.setSize(150, 25);
		txtStart.setLocation(100, 10);
		txtStart.setEditable(false);

		labelEnd = new JLabel("Endtime");
		panelPrescription.add(labelEnd);
		labelEnd.setSize(120, 25);
		labelEnd.setLocation(10, 40);

		txtEnd = new JTextField();
		panelPrescription.add(txtEnd);
		txtEnd.setSize(150, 25);
		txtEnd.setLocation(100, 40);
		txtEnd.setEditable(false);

		labelDrug = new JLabel("Drug");
		panelPrescription.add(labelDrug);
		labelDrug.setSize(120, 25);
		labelDrug.setLocation(10, 70);

		txtDrug = new JTextField();
		panelPrescription.add(txtDrug);
		txtDrug.setSize(150, 25);
		txtDrug.setLocation(100, 70);
		txtDrug.setEditable(false);

		labelDayDose = new JLabel("Day dose");
		panelPrescription.add(labelDayDose);
		labelDayDose.setSize(120, 25);
		labelDayDose.setLocation(10, 100);

		txtDayDose = new JTextField();
		panelPrescription.add(txtDayDose);
		txtDayDose.setSize(150, 25);
		txtDayDose.setLocation(100, 100);
		txtDayDose.setEditable(false);

		labelTotalDose = new JLabel("Total dose");
		panelPrescription.add(labelTotalDose);
		labelTotalDose.setSize(120, 25);
		labelTotalDose.setLocation(10, 130);

		txtTotalDose = new JTextField();
		panelPrescription.add(txtTotalDose);
		txtTotalDose.setSize(150, 25);
		txtTotalDose.setLocation(100, 130);
		txtTotalDose.setEditable(false);

		panelPN = new JPanel();
		panelPN.setLayout(null);
		this.add(panelPN);
		panelPN.setSize(400, 300);
		panelPN.setLocation(300, 200);
		panelPN.setVisible(false);

		labelPNDose = new JLabel("Dose");
		panelPN.add(labelPNDose);
		labelPNDose.setSize(120, 25);
		labelPNDose.setLocation(10, 10);

		txtPNDose = new JTextField();
		panelPN.add(txtPNDose);
		txtPNDose.setSize(150, 25);
		txtPNDose.setLocation(100, 10);
		txtPNDose.setEditable(false);

		labelUsed = new JLabel("Given");
		panelPN.add(labelUsed);
		labelUsed.setSize(120, 25);
		labelUsed.setLocation(10, 40);

		txtUsed = new JTextField();
		panelPN.add(txtUsed);
		txtUsed.setSize(150, 25);
		txtUsed.setLocation(100, 40);
		txtUsed.setEditable(false);

		txtUsedDate = new JTextField();
		panelPN.add(txtUsedDate);
		txtUsedDate.setSize(80, 25);
		txtUsedDate.setLocation(10, 80);
		txtUsedDate.setText("dd-mm-yyyy");
		txtUsedDate.setEditable(true);
		// controls...
		usePN = new JButton();
		usePN.setLocation(100, 80);
		usePN.setSize(140, 25);
		usePN.setText("Use prescription");
		usePN.addActionListener(controller);
		panelPN.add(usePN);

		panelRegular = new JPanel();
		panelRegular.setLayout(new GridLayout(2, 4, 1, 1));
		this.add(panelRegular);
		panelRegular.setSize(330, 90);
		panelRegular.setLocation(260, 200);
		panelRegular.add(new JLabel("    Morning"));
		panelRegular.add(new JLabel("        Noon"));
		panelRegular.add(new JLabel("    Evening"));
		panelRegular.add(new JLabel("       Night"));
		txtMorning = new JTextField();
		txtNoon = new JTextField();
		txtEvening = new JTextField();
		txtNight = new JTextField();
		panelRegular.add(txtMorning);
		panelRegular.add(txtNoon);
		panelRegular.add(txtEvening);
		panelRegular.add(txtNight);
		panelRegular.setVisible(false);

		panelIrregular = new JPanel();
		panelIrregular.setLayout(null);
		this.add(panelIrregular);
		panelIrregular.setSize(330, 90);
		panelIrregular.setLocation(300, 200);

		listDoses = new JList();
		scrollDoses = new JScrollPane(listDoses);
		panelIrregular.add(scrollDoses);
		scrollDoses.setLocation(0, 0);
		scrollDoses.setSize(140, 90);

		panelIrregular.setVisible(false);

		errorLabel = new JLabel("");
		errorLabel.setSize(250, 22);
		errorLabel.setLocation(20, 280);
		this.add(errorLabel);
		errorLabel.setForeground(Color.RED);

		listPrescriptions.addListSelectionListener(controller);
	}

	private class Controller implements ActionListener, ListSelectionListener {
		private Service service = Service.getInstance();

		public void actionPerformed(ActionEvent e) {
			errorLabel.setText("");
			if (e.getSource() == usePN) {
				try {
					GregorianCalendar anvendtDato = Validation.makeDate(txtUsedDate.getText());
					service.prescriptionPNUsed((PN) currentPrescription, anvendtDato);
					txtUsed.setText(((PN) currentPrescription).getNumberOfTimesGiven() + " times");
					txtPNDose.setText(((PN) currentPrescription).getCountOfUnits() + "");

					txtDayDose.setText(currentPrescription.dayDose() + " "
							+ currentPrescription.getDrug().getUnit());
					txtTotalDose.setText(currentPrescription.totalDose() + " "
							+ currentPrescription.getDrug().getUnit());
				} catch (RuntimeException ex) {
					errorLabel.setText(ex.getMessage());
				}
			}
		}

		public void valueChanged(ListSelectionEvent e) {
			panelPN.setVisible(false);
			panelRegular.setVisible(false);
			panelIrregular.setVisible(false);
			if (listPrescriptions.isSelectionEmpty()) {
			} else {
				panelPrescription.setVisible(true);
				currentPrescription = (Prescription) listPrescriptions.getSelectedValue();
				txtStart.setText(currentPrescription.getStartDate().get(GregorianCalendar.DATE)
						+ "."
						+ (currentPrescription.getStartDate().get(GregorianCalendar.MONTH) + 1)
						+ "." + currentPrescription.getStartDate().get(GregorianCalendar.YEAR));
				txtEnd.setText(currentPrescription.getEndDate().get(GregorianCalendar.DATE) + "."
						+ (currentPrescription.getEndDate().get(GregorianCalendar.MONTH) + 1) + "."
						+ currentPrescription.getEndDate().get(GregorianCalendar.YEAR));
				txtDrug.setText(currentPrescription.getDrug().getName() + " ("
						+ currentPrescription.getDrug().getUnit() + ")");
				txtDayDose.setText(currentPrescription.dayDose() + " "
						+ currentPrescription.getDrug().getUnit());
				txtTotalDose.setText(currentPrescription.totalDose() + " "
						+ currentPrescription.getDrug().getUnit());
				if (currentPrescription instanceof PN) {
					panelPN.setVisible(true);
					txtUsed.setText(((PN) currentPrescription).getNumberOfTimesGiven() + " times");
					txtPNDose.setText(((PN) currentPrescription).getCountOfUnits() + "");
				} else if (currentPrescription instanceof DailyRegular) {
					panelRegular.setVisible(true);
					if (((DailyRegular) currentPrescription).getDoses()[0] != null)
						txtMorning.setText(((DailyRegular) currentPrescription).getDoses()[0]
								.getCount()
								+ "");
					if (((DailyRegular) currentPrescription).getDoses()[1] != null)
						txtNoon.setText(((DailyRegular) currentPrescription).getDoses()[1]
								.getCount()
								+ "");
					if (((DailyRegular) currentPrescription).getDoses()[2] != null)
						txtEvening.setText(((DailyRegular) currentPrescription).getDoses()[2]
								.getCount()
								+ "");
					if (((DailyRegular) currentPrescription).getDoses()[3] != null)
						txtNight.setText(((DailyRegular) currentPrescription).getDoses()[3]
								.getCount()
								+ "");

				} else {
					panelIrregular.setVisible(true);
					listDoses.setListData(((DailyIrregular) currentPrescription).getDoses()
							.toArray());
				}
			}
		}
	}
}
